package base.Chapter00_Structures.C04_Stack;


import java.util.Arrays;

/**
 * 栈数据结构
 * 数组实现
 * <p>
 * 属性列表：
 * 1， 容量： capacity
 * 2， 当前栈顶指针：pointor
 * 初始值指向哪里？
 * 0，即下一个插入位置
 * <p>
 * 功能列表：
 * 1， 压栈
 * 判断栈是否满？
 * pointor==capacity
 * 满
 * 抛出exception
 * 不满
 * 赋值
 * 移动栈顶指针到下一个
 * <p>
 * 2， 弹栈
 * 判断是否到了栈底
 * 空
 * 抛出exception
 * 不空
 * pointor-1
 *
 * 3,清空栈
 *  pointor归零
 */
public class Stack {
    private final int capacity;
    private int pointor;
    private final Object[] container;

    public Stack(int capacity) {
        if (capacity <= 0) {
            try {
                throw new Exception("Stack init failture!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.capacity = capacity;
        this.pointor = 0;
        this.container = new Object[capacity];
    }

    public boolean push(Object obj) {
        boolean isSuccess = false;
        if (pointor < capacity) {
            container[pointor++] = obj;
        }
        return isSuccess;
    }

    public Object pop() {
        Object obj = null;
        if (pointor > 0)
            obj = container[--pointor];
        return obj;
    }

    public void clean() {
        pointor = 0;
    }

    public void printStack() {
        System.out.println(Arrays.toString(container));
    }
}
